package demo11_ThreadPool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author kunkun
 * @className Test01_ThreeMethods
 * @Description Executors的三大方法
 * @date 2025/3/6 16:34
 */
public class Test01_ThreeMethods {
    public static void main(String[] args) {
        //1. 创建线程池
//        ExecutorService service = Executors.newSingleThreadExecutor();      //创建一个线程大小的线程池--单个线程执行
//        ExecutorService service = Executors.newFixedThreadPool(5);     //创建一个线程数目为5的线程池--5个线程共同执行
        ExecutorService service = Executors.newCachedThreadPool();      //创建一个可扩展的线程池--遇强则强，遇弱则弱，线程数目不固定

        //2. 线程执行
        try {
            for (int i = 0; i < 10; i++) {
                final int index = i;
                service.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"-->"+index);
                });
            }
        } finally {
            //3. 关闭线程池
            service.shutdown();
        }

    }
}
